/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Comp_11_FileChooser.java]
 * Creation Date        [23-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing.components;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileView;

/**
 * Swing 文件选择组件
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Comp_11_FileChooser {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ImageViewFrame frame = new ImageViewFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 图片预览窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ImageViewFrame extends JFrame {

	private static final long serialVersionUID = 3121090809528906333L;

	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 400;

	private JLabel label = null;
	private JFileChooser chooser = null;

	/**
	 * 构造方法
	 */
	public ImageViewFrame() {
		setTitle("Image Preview");
		this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLocationByPlatform(true);
		setResizable(false);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);

		// "打开"菜单
		JMenuItem openItem = new JMenuItem("Open");
		fileMenu.add(openItem);
		openItem.addActionListener(new FileOpenListener());

		// "退出"菜单
		JMenuItem exitItem = new JMenuItem("Exit");
		fileMenu.add(exitItem);
		exitItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		label = new JLabel();
		getContentPane().add(label);

		chooser = new JFileChooser();
		// JDK6.0新增类, 设置文件过滤器
		FileNameExtensionFilter filter = new FileNameExtensionFilter(
				"Image Files", "jpg", "jpeg", "gif");
		chooser.setFileFilter(filter);
		// 在每个文件之前添加一个图标
		chooser.setFileView(new FileIconView(filter, new ImageIcon(
				"src_01_language_basic/pkg_resources/pic_02.gif")));
		// 设置图像预览
		chooser.setAccessory(new ImagePreview(chooser));
	}

	/**
	 * 文件打开监听器
	 * 
	 * @author 不落的太阳(Sean Yang)
	 * @version 1.0
	 * @since JDK 6
	 */
	private class FileOpenListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			chooser.setCurrentDirectory(new File("."));
			int result = chooser.showOpenDialog(ImageViewFrame.this);
			if (result == JFileChooser.APPROVE_OPTION) {
				String fileName = chooser.getSelectedFile().getPath();
				label.setIcon(new ImageIcon(fileName));
			}
		}
	}
}

/**
 * 为每个文件之前添加一个图标
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class FileIconView extends FileView {

	private FileNameExtensionFilter fileFilter = null;
	private Icon icon = null;

	/**
	 * 构造方法
	 * 
	 * @param filter
	 *            文件过滤器
	 * @param icon
	 *            图标
	 */
	public FileIconView(FileNameExtensionFilter filter, Icon icon) {
		fileFilter = filter;
		this.icon = icon;
	}

	/**
	 * 添加图标
	 * 
	 * @param file
	 *            需要添加图标的文件
	 */
	@Override
	public Icon getIcon(File file) {
		if (!file.isDirectory() && fileFilter.accept(file)) {
			return icon;
		} else {
			return null;
		}
	}
}

/**
 * 文件预览
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ImagePreview extends JLabel {

	private static final long serialVersionUID = -2759389674414088508L;

	/**
	 * 构造方法
	 * 
	 * @param chooser
	 *            文件选择器
	 */
	public ImagePreview(JFileChooser chooser) {
		setPreferredSize(new Dimension(100, 100));
		setBorder(BorderFactory.createEtchedBorder());

		chooser.addPropertyChangeListener(new PropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if (event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
					File file = (File) event.getNewValue();
					if (file == null) {
						setIcon(null);
						return;
					}

					ImageIcon icon = new ImageIcon(file.getPath());
					if (icon.getIconWidth() > getWidth()) {
						icon = new ImageIcon(icon.getImage().getScaledInstance(
								getWidth(), -1, Image.SCALE_DEFAULT));
					}
					setIcon(icon);
				}
			}
		});
	}
}
